/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.common.dao.UserDAO;
import com.javaeye.common.dto.User;
import com.javaeye.common.util.StringUtils;

public class UserService implements IUserService {
	
	private static Log log = LogFactory.getLog(UserService.class);
	
	private UserDAO dao;
	
	public void setDao(UserDAO dao) {
		this.dao = dao;
	}

	@Override
	public User getUser(String id) {
		int intId = Integer.parseInt(id);
		User user = dao.getUser(intId);
		if (user == null) {
			log.warn("在数据库中未找到指定的用户  id：" + id);
		}
		return user;
	}

	@Override
	public List<User> getUsers() {
		List<User> results = dao.getUsers();
		if (results == null || results.size() == 0) {
			log.warn("在数据库中未找到用户数据");
		}
		return results;
	}

	@Override
	public boolean removeUser(String id) {
		int intId = Integer.parseInt(id);
		dao.removeUser(intId);
		return true;
	}

	@Override
	public User saveUser(User user) {
		dao.saveUser(user);
		return user;
	}
	
	public User login(User user) {
		user.setPassword(StringUtils.password(user.getPassword()));
		return dao.getLoginUser(user);
	}
	
	public long getUserByName(String userName) {
		return dao.getUserByName(userName);
	}

}
